/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package huang.fb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author huang
 */
public class SearchTimeServlet extends HttpServlet {
    private ServletContext context;
    private BusStationData bsData = new BusStationData();
    private HashMap busStations = bsData.getBusStation();
    
    private String startStation;
    private String destinationStation;
    private String desireTime;
    
    private TimeTable tTable = new TimeTable();
    private HashMap<String, String> f1Table = tTable.getF1TimeTable();
    private HashMap<String, String> f2Table = tTable.getF2TimeTable();
    private HashMap<Integer, String> durationTable = tTable.getDurationTable();
    private Integer desireMinute;
    private Integer desireHour;
    private String nextBus;
    private String comesAt;
    private String duration;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        StringBuffer sb = new StringBuffer();
        
        startStation = request.getParameter("startStation");
        destinationStation = request.getParameter("destinationStation");
        desireTime = request.getParameter("desireTime");
        
        desireMinute = Integer.valueOf(desireTime.substring(desireTime.length()-2));
        desireHour = Integer.valueOf(desireTime.substring(desireTime.length()-5, desireTime.length()-3));
  
        checkTimeTable();
        calculateDuration();
        
        sb.append("<result>");
        sb.append("<startStation>").append(startStation).append("</startStation>");
        sb.append("<destinationStation>").append(destinationStation).append("</destinationStation>");
        sb.append("<nextBus>").append(nextBus).append("</nextBus>");
        sb.append("<comesAt>").append(comesAt).append("</comesAt>");
        sb.append("<duration>").append(duration).append("</duration>");
        sb.append("</result>");
          
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        response.getWriter().write("<resultpage>" + sb.toString() + "</resultpage>");
       
    }
    private void calculateDuration(){
   
        int startMin = 0;
        int endMin = 0;
        for (int time : durationTable.keySet()) {
            String station = durationTable.get(time);
            if(station.equals(startStation)){
                startMin = time;
                
            }
            if(station.equals(destinationStation)){
                endMin = time;
            }
            System.out.println(endMin);
            System.out.println(startMin);
        }
        duration = String.valueOf(endMin - startMin);
    }
    
    private void checkTimeTable(){
        comesAt = "about ";
        if(desireHour >= 18){
              
               comesAt = "tommorow about ";
               desireHour = 12;
               desireMinute = 0;
               checkF1Table();
           }else if(desireHour < 12){
               desireHour = 12;
               desireMinute = 0;
               checkF1Table();
           }else{
               checkF1Table();
           }
        
    }
    
    private void checkF1Table(){
        for (String time : f1Table.keySet()) {
            String stopName = f1Table.get(time);
            Integer hour = Integer.valueOf(time.substring(0, 2));
            Integer minute = Integer.valueOf(time.substring(3, 5));
            if(stopName.equals(startStation)){
                
                if(desireHour.equals(hour) ){
                    comesAt = comesAt.concat(String.valueOf(hour));
                    
                    if(desireMinute > minute){
                        
                        checkF2Table();
                        break;
                    } else {
                        comesAt = comesAt.concat(":").concat( (minute<=9) ? ("0".concat(String.valueOf(minute))) : (String.valueOf(minute)));
                        nextBus = "F1";
                        break;
                    }
                }
                
            }
        }
    }
    
    private void checkF2Table(){
        for (String time : f2Table.keySet()) {
            String stopName = f2Table.get(time);
            Integer hour = Integer.valueOf(time.substring(0, 2));
            Integer minute = Integer.valueOf(time.substring(3, 5));
            if(stopName.equals(startStation)){
                if(desireHour.equals(hour)){
                    if(desireMinute <= minute){
                        comesAt = comesAt.concat(":").concat( (minute<=9) ? ("0".concat(String.valueOf(minute))) : (String.valueOf(minute)));
                        nextBus = "F2";
                        break;
                    }else if((desireMinute > minute) && (desireHour <= 16)){
                        
                        comesAt = "about ";
                        
                        desireMinute = 0;
                        desireHour += 1;
                        checkF1Table();
                        break;
                    }else if(desireHour > 16){
                        comesAt = "tommorow about ";
                        desireMinute = 0;
                        desireHour = 12;
                        checkF1Table();
                        break;
                    }
                   
                }
                
            }
        }
    }
    
    

    

}
 